<template>
  <div class="operation-log">
    <!-- 页面标题 -->
    <div class="page-header">
      <h2>操作日志</h2>
    </div>

    <!-- 操作按钮区域 -->
    <div class="toolbar">
      <el-button
        type="danger"
        icon="el-icon-delete"
        :disabled="logList.length === 0"
        @click="handleClearAllLogs"
      >
        清空所有日志
      </el-button>
    </div>

    <!-- 日志表格 -->
    <el-table
      :data="logList"
      v-loading="loading"
      stripe
      border
      style="width: 100%"
      empty-text="暂无操作日志"
    >
      <el-table-column prop="operator" label="操作用户" align="center" />

      <el-table-column prop="module" label="操作模块" " align="center" />

      <el-table-column prop="extJson" label="是否异常" align="center" />

      <

      <el-table-column prop="operationTime" label="操作时间" align="center" />

      <el-table-column prop="operationTime" label="path" align="center" />

      <el-table-column label="操作" width="100" align="center" fixed="right">
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            style="color: #f56c6c"
            @click="handleDeleteLog(scope.row, scope.$index)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <div class="pagination-wrapper" v-if="total > pageSize">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      />
    </div>
  </div>
</template>

<script>
import { getData, deletelog, clearAll } from "@/api/operation"; // 根据你的实际路径调整
export default {
  name: "OperationLog",
  data() {
    return {
      loading: false,
      logList: [], // 日志列表数据
      currentPage: 1,
      pageSize: 10,
      total: 0, // 总条数
    };
  },
  mounted() {
    this.loadLogData(); // 页面加载时获取数据;
  },
  methods: {
    // 加载日志数据（使用假数据）
    async loadLogData() {
      this.loading = true;
      try {
        const params = {
          page: this.currentPage,
          pageSize: this.pageSize,
          // 可选：如果需要搜索，可以加上 keyword
          // keyword: this.searchKeyword
        };

        const response = await getData(params); // 调用 API 获取数据
        console.log("获取日志数据:", response.data);
        if (response.data.code === 200) {
          this.logList = response.data.data.ystemOperationLog.records || [];
          this.total = response.data.data.ystemOperationLog.total || 0;
        } else {
          this.$message.error(response.message || "获取日志失败");
        }
      } catch (error) {
        console.error("获取日志失败:", error);
        this.$message.error("获取日志失败，请稍后重试");
      } finally {
        this.loading = false;
      }
    },

    // 删除单条日志
    // ✅ 删除单条日志
    handleDeleteLog(row, index) {
      this.$confirm(
        `确定要删除这条操作日志吗？（用户：${row.operator}，操作模块：${row.module}）`,
        "确认删除",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(async () => {
          try {
            const params = { id: row.id }; // 假设后端需要 id 参数
            const response = await deletelog(row.id);

            if (response.data.code === 200) {
                this.loadLogData();
        
              this.$message.success("删除成功！");
            } else {
              this.$message.error(response.message || "删除失败");
            }
          } catch (error) {
            console.error("删除日志失败:", error);
            this.$message.error("删除失败，请稍后重试");
          }
        })
        .catch(() => {
          this.$message.info("已取消删除");
        });
    },

    // ✅ 清空所有日志
    handleClearAllLogs() {
      this.$confirm("确定要清空所有操作日志吗？此操作不可恢复！", "确认清空", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          try {
            const response = await clearAll({}); // 假设清空不需要额外参数
            if (response.data=code === 200) {
            
              this.logList = []; // 清空列表
              this.total = 0;
              this.$message.success("所有日志已清空！");
              this.loadLogData()
            } else {
              this.$message.error(response.message || "清空失败");
            }
          } catch (error) {
            console.error("清空日志失败:", error);
            this.$message.error("清空失败，请稍后重试");
          }
        })
        .catch(() => {
          this.$message.info("已取消清空");
        });
    },

    // ✅ 分页相关方法（保持不变）
    handleSizeChange(newSize) {
      this.pageSize = newSize;
      this.currentPage = 1;
      this.loadLogData();
    },
    handleCurrentChange(newPage) {
      this.currentPage = newPage;
      this.loadLogData();
    },
  },
};
</script>

<style scoped>
.operation-log {
  padding: 20px;
  background: #fff;
}

.page-header {
  margin-bottom: 20px;
}

.page-header h2 {
  margin: 0;
  color: #303133;
  font-size: 24px;
  font-weight: 500;
}

.toolbar {
  margin-bottom: 20px;
  padding: 15px 0;
}

.pagination-wrapper {
  margin-top: 20px;
  text-align: right;
}

/* 表格样式优化 */
.el-table {
  font-size: 14px;
}

.el-table th {
  background-color: #fafafa;
  color: #606266;
  font-weight: 600;
}

/* 标签样式 */
.el-tag {
  font-size: 12px;
}

/* 按钮样式 */
.el-button--text {
  font-size: 13px;
}

/* 响应式处理 */
@media (max-width: 768px) {
  .operation-log {
    padding: 10px;
  }

  .pagination-wrapper {
    text-align: center;
  }
}
</style>